package com.gdones.bootserver.bus.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.bootserver.bus.innerCode.mapper.InnerCodeMapper;
import com.gdones.bootserver.bus.menu.dto.Menu;
import com.gdones.bootserver.bus.menu.mapper.MenuMapper;
import com.gdones.bootserver.bus.menu.service.IMenuService;
import com.gdones.common.core.dto.MyResult;
import com.gdones.bootserver.core.util.MySysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统菜单信息表 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-03-27
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    /**
     * 注入innerCode生成工具相关的mybatis实例
     */
    @Autowired
    private InnerCodeMapper innerCodeMapper;

    @Override
    public MyResult getList(Menu menu) {
        MyResult result = new MyResult();

        MenuMapper baseMapper = getBaseMapper();

        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();

        menuQueryWrapper.orderByAsc("orderIndex");

        List<Menu> menus = baseMapper.selectList(menuQueryWrapper);

        result.setData(menus);

        return result;
    }

    @Override
    public MyResult addMenu(Menu menu) {
        MyResult result = new MyResult();

        MenuMapper baseMapper = getBaseMapper();

        //先查询有没有相同编号的数据
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("code",menu.getCode());

        List<Menu> menus = baseMapper.selectList(queryWrapper);

        if(menus.size()>0){
            result.setMsg("编号["+menu.getCode()+"]重复，请重新换一个编号！");
            result.setCode("9999");
            return result;
        }

        //创建内部编码
        String innerCode = MySysUtil.getInnerCode("t_menu", "menuID",menu.getParentID());


        menu.setInnerCode(innerCode);
        menu.setIsDelete(0);

        int insert = baseMapper.insert(menu);

        if(insert>0){
            result.setMsg("添加成功!");
            result.setData(menu);
        }else{
            result.setMsg("添加失败!");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult deleteMenu(String innerCode) {
        MyResult result = new MyResult();

        UpdateWrapper<Menu> deleteWrapper = new UpdateWrapper<>();

        deleteWrapper.like("innercode",innerCode+"%");

        MenuMapper baseMapper = getBaseMapper();

        int delete = baseMapper.delete(deleteWrapper);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult updateMenu(Menu menu) {
        MyResult result = new MyResult();

        MenuMapper menuMapper = getBaseMapper();

        //先查询出旧的组织信息
        Menu oldMenu = menuMapper.selectById(menu.getMenuID());
        //如果父级没有发生变化内部编码就不改变
        if(!(oldMenu.getParentID().equals(menu.getParentID()))){

            //生成新的内部编码
            String innerCode = MySysUtil.getInnerCode("t_menu", "menuID",menu.getParentID());
            menu.setInnerCode(innerCode);

            int update = menuMapper.updateById(menu);

            //修改子集数据
            int i = menuMapper.updateSonsInnerCode(oldMenu.getInnerCode(), innerCode);

            if(update>0){
                result.setMsg("修改成功！");
            }else{
                result.setMsg("修改失败！");
                result.setCode("9999");
            }
        }else{
            int update = menuMapper.updateById(menu);

            if(update>0){
                result.setMsg("修改成功！");
            }else{
                result.setMsg("修改失败！");
                result.setCode("9999");
            }
        }
        return result;
    }

    @Override
    public MyResult getMenuDataByID(String menuID) {
        MyResult result = new MyResult();

        MenuMapper baseMapper = getBaseMapper();

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("menuID",menuID);

        Menu menu = baseMapper.selectOne(queryWrapper);

        if(menu!=null){
            result.setMsg("查询成功！");
            result.setData(menu);
        }else{
            result.setMsg("查询失败！");
            result.setCode("9999");
        }

        return result;
    }
}
